<?php

namespace App\Repositories\Workorder;

use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Models\Workorder\Workorder;
use App\Validators\Workorder\WorkorderValidator;

/**
 * Class WorkorderRepositoryEloquent.
 *
 * @package namespace App\Repositories\Workorder;
 */
class WorkorderRepositoryEloquent extends BaseRepository implements WorkorderRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Workorder::class;
    }

    /**
    * Specify Validator class name
    *
    * @return mixed
    */
    public function validator()
    {

        return WorkorderValidator::class;
    }


    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * 获取工单订单列表
     * @author Min
     * @param int $user_id
     * @param string $keyword
     * @param int $page_size
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getWorkOrderList($user_id=5960, $keyword='', $page_size=10)
    {

        $field = ['og.goods_name','o.order_status','o.user_id','o.order_id','o.order_sn','o.add_time','o.confirm_time'];

        $model = DB::table('order_info as o');

        if(!empty($keyword)){
            $model = $model->where(function ($query) use ($keyword){
                $query->where('o.order_sn', 'like', "%$keyword%")->orWhere('og.goods_name', 'like', "%$keyword%");
            });
        }

        $data = $model->leftJoin('order_goods as og', 'o.order_id', '=', 'og.order_id')
            ->select($field)
            ->where('o.user_id', $user_id)
            ->where('o.shipping_status', 2)
            ->whereIn('o.order_status', [1, 5])
            ->groupBy('o.order_id')
            ->orderBy('o.order_id', 'desc')
            ->paginate($page_size);

        return $data;
    }

    /**
     * 获取工单订单商品列表
     * @author Min
     * @param int $order_id
     * @return mixed
     */
    public function getOrderGoodsList($order_id = 0){
        $where = array(
            'og.order_id' => $order_id
        );

        $field = [
            'og.goods_id',
            'og.goods_number',
            'og.giving',
            'og.goods_price',
            'g.goods_name',
            'g.goods_thumb',
            'g.goods_img',
            'g.cat_id'
        ];

        $data = DB::table('order_goods as og')
            ->leftJoin('goods as g', 'og.goods_id', '=', 'g.goods_id')
            ->select($field)
            ->where($where)
            ->get()->toArray();

        return array_map('get_object_vars', $data);
    }

    /**
     * 获取售后工单详情
     * @author Min
     * @param int $uid
     * @param int $order_id
     * @param int $goods_id
     * @param int $workorder_id
     * @return mixed
     */
    public function getWorkorderInfo($uid=0, $order_id=0, $goods_id=0, $workorder_id=0){

        $order_id = 544793;
        $goods_id = 510;
        $uid = 2125;

        $where = [
            'uid' => $uid,
            'order_id' => $order_id,
            'goods_id' => $goods_id,
            'id' => $workorder_id
        ];

        if($order_id == 0) unset($where['order_id']);
        if($goods_id == 0) unset($where['goods_id']);
        if($workorder_id == 0) unset($where['id']);

        $data = DB::table('workorder')
            ->where($where)
            ->where('status', '!=', 4)
            ->first();

        return collect($data)->toArray();
    }


    
}
